package com.tx.sqlway;
import java.sql.*;

public class Sql  {

	  Connection con;
	  public Sql() {
		  try{
			  Class.forName("com.mysql.jdbc.Driver");
		  }
		  catch(Exception e)
		  {
			  System.out.print("false"); 
		  }  
	  }
	public Connection getcon() throws Exception{
		
		con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mywork?characterEncoding=utf8&user=root&password=123456789");
          return con;		
	}
	public int exquesql(String sql,Object[] obj) throws Exception{
		con=getcon();
		PreparedStatement st=con.prepareStatement(sql);
		for(int i=0;i<obj.length;i++)
		{
			st.setObject(i+1,obj[i]);
		}
		int rs=st.executeUpdate();
		st.close();
		con.close();
		return rs;
	}
	
	public ResultSet getsql(String sql,Object[] obj) throws Exception{
		con=getcon();
		PreparedStatement st=con.prepareStatement(sql);
		if(obj!=null){
			for(int i=0;i<obj.length;i++)
			{		
				st.setObject(i+1,obj[i]);
			}
		}
		ResultSet rs=st.executeQuery();
		
		return rs;
	}
	
	
	
	
	public boolean exque(String sql,Object[] obj){
		
		try {
			con=getcon();
			PreparedStatement st=con.prepareStatement(sql);
			for(int i=0;i<obj.length;i++)
			{
				
				st.setObject(i+1,obj[i]);
			}
			ResultSet rs=st.executeQuery();
			if(rs.next())
			{
				return true;
			}
		
			return false;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
	
}
